package spring.angularjs.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.angularjs.domain.Status;
import spring.angularjs.domain.Userinfo;
import spring.angularjs.domain.json.UserTemp;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value="/changestatus")
public class ChangeStatusController {
	
	@Autowired
	private Services services;
	
	@RequestMapping(value="/start")
	public String goStatusChangePage(){
		return "detail/changestatus";
	}

	// not using headers=application/json
	@RequestMapping(value="/getlist", method=RequestMethod.GET)
	@ResponseBody
	public List<UserTemp> getListUser(){
		List<Userinfo> list = services.userInfos();
		List<UserTemp> list_temp = new ArrayList<UserTemp>();
		for (int i = 0; i < list.size(); i++) {
			Userinfo user = list.get(i);
			UserTemp userTemp = new UserTemp();
			if(user.getImage() != null){
				userTemp = new UserTemp(user.getLoginId(), user.getStatus()
						.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
						user.getCardnumberid(), user.getFirstname(),
						user.getLastname(), user.getMidname(), user.getPhone1(),
						user.getPhone2(), user.getAddress1(), user.getAddress2(),
						user.getEmail1(), user.getEmail2(), new String(user.getImage()));
			} else {
				userTemp = new UserTemp(user.getLoginId(), user.getStatus()
						.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
						user.getCardnumberid(), user.getFirstname(),
						user.getLastname(), user.getMidname(), user.getPhone1(),
						user.getPhone2(), user.getAddress1(), user.getAddress2(),
						user.getEmail1(), user.getEmail2());
			}
			list_temp.add(userTemp);
		}
		return list_temp;
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.POST)
	@ResponseBody
	public UserTemp getUserInfo(@PathVariable("id") String id){
		Userinfo user = services.getUserInfo(id);
		UserTemp userTemp = new UserTemp();
		if(user.getImage() != null){
			userTemp = new UserTemp(user.getLoginId(), user.getStatus()
					.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
					user.getCardnumberid(), user.getFirstname(),
					user.getLastname(), user.getMidname(), user.getPhone1(),
					user.getPhone2(), user.getAddress1(), user.getAddress2(),
					user.getEmail1(), user.getEmail2(), new String(user.getImage()));
		} else {
			userTemp = new UserTemp(user.getLoginId(), user.getStatus()
					.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
					user.getCardnumberid(), user.getFirstname(),
					user.getLastname(), user.getMidname(), user.getPhone1(),
					user.getPhone2(), user.getAddress1(), user.getAddress2(),
					user.getEmail1(), user.getEmail2());
		}
		return userTemp;
	}
	
	@RequestMapping(value="/active/{id}", method=RequestMethod.POST)
	@ResponseBody
	public void activeUser(@PathVariable("id") String id){
		HomeController.page = "changestatus";
		Userinfo user = services.getUserInfo(id);
		Status status = services.getStatus("status02");
		user.setStatus(status);
		services.update(user);
	}
	
	@RequestMapping(value="/disable/{id}", method=RequestMethod.POST)
	@ResponseBody
	public void disableUser(@PathVariable("id") String id){
		HomeController.page = "changestatus";
		Userinfo user = services.getUserInfo(id);
		Status status = services.getStatus("status03");
		user.setStatus(status);
		services.update(user);
	}
}
